package com.wisemoresystem.seal.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.page.Page;
import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_log;
import com.wisemoresystem.seal.model.t_log.Level;
import com.wisemoresystem.seal.util.DateTools;
import com.wisemoresystem.seal.util.SpringTools;
import com.wisemoresystem.seal.util.ip.IPTools;
import com.wisemoresystem.seal.web.WebTools;

@Service
public class LogService extends BaseService {
	@Autowired
	private BankService service;

	@Transactional
	public void logH(String logStr, Class<?> clazz, String clazz_id) {
		log(logStr, clazz, clazz_id, Level.H);
	}

	@Transactional
	public void logM(String logStr, Class<?> clazz, String clazz_id) {
		log(logStr, clazz, clazz_id, Level.M);
	}

	@Transactional
	public void logL(String logStr, Class<?> clazz, String clazz_id) {
		log(logStr, clazz, clazz_id, Level.L);
	}

	public void log(String logStr, Class<?> clazz, String clazz_id, Level level) {
		t_log log = new t_log();
		log.setLog(logStr);
		log.setClazz(clazz.toString());
		log.setClazz_id(clazz_id);
		log.setUser_id(WebTools.getCurrentUserId());
		log.setLevel(level.toString());
		log.setCompany_id(WebTools.getCurrentBankId());
		log.setIp(IPTools.getIP());
		ht.save(log);
	}

	@Transactional
	public void logLL(String logStr) {
		logLL(logStr, null);
	}

	@Transactional
	public void logLL(String logStr, String user_id) {
		t_log log = new t_log();
		log.setLog(logStr);
		log.setUser_id(user_id);
		log.setLevel(Level.L.toString());
		// log.setCompany_id(WebTools.getCurrentBankId());
		ht.save(log);
	}

	public List<Map<String, Object>> selectOperatreLog(String srh_uname,
			String srh_name, String srh_start, Page page) {
		String ids = service.selectUbank2();
		if (ChkTools.isNull(ids)) {
			ids = "''";
		}
		try {
			String sql = "select t.*,tu.uname operator_name,tb.name bname from @t_log t "
					+ " left join t_user tu on t.user_id=tu._id "
					+ " left join t_bank tb on tu.bank_id=tb._id "
					+ " where t.status=0 and tb._id in(@ids)"
					+ " and t.log like ? and tu.uname like ? "
					+ " order by t.created desc";
			if (ChkTools.isNull(srh_start)) {
				sql = sql.replace("@t_log", "t_log");
			} else {
				sql = sql.replace("@t_log", "t_log_" + srh_start);
			}
			List<String> params = new ArrayList<String>();
			if (ChkTools.isNull(srh_name)) {
				sql = sql.replace("and t.log like ?", "");
			} else {
				params.add("%" + srh_name + "%");
			}
			if (ChkTools.isNull(srh_uname)) {
				sql = sql.replace("and tu.uname like ?", "");
			} else {
				params.add("%" + srh_uname + "%");
			}
			sql = sql.replace("@ids", ids);
			List<Map<String, Object>> list = jc.queryForList(sql,
					params.toArray(), page);
			return list;
		} catch (Exception e) {
			return null;
		}
	}

	public List<Map<String, Object>> selectOperatreLog2(String srh_user_name,
			String srh_name, Page page) {
		String sql = "select t.*,tu.uname,tu.name from t_log t "
				+ " left outer join t_user tu on t.user_id=tu._id "
				+ " where t.status=0 and tu.uname like ? and t.log like ? "
				+ " order by t.created desc";
		List<String> params = new ArrayList<String>();
		if (ChkTools.isNull(srh_user_name)) {
			sql = sql.replace("and tu.uname like ?", "");
		} else {
			params.add("%" + srh_user_name + "%");
		}
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and t.log like ?", "");
		} else {
			params.add("%" + srh_name + "%");
		}
		// List<Map<String, Object>> list = jc.queryForList(sql,
		// params.toArray(), page);
		return null;
	}

	// 从t_log表中找出上月的记录，生成t_log_YYYYMM表将数据存入，同时删除原数据
	@Transactional
	private void selectAndCreate(String table, String time) {
		String insert_sql = "insert into @t_log select * from t_log where substring(convert(varchar(10),created,112),1,6)='@time'";
		insert_sql = insert_sql.replace("@t_log", table);
		insert_sql = insert_sql.replace("@time", time);
		jt.execute(insert_sql);
		String delete_sql = " delete from t_log where substring(convert(varchar(10),created,112),1,6)='@time'";
		delete_sql = delete_sql.replace("@time", time);
		jt.execute(delete_sql);

	}

	public void createTable() {
		String time = DateTools.getLastYearMonth();
		String sql = "select * from t_log where substring(convert(varchar(10),created,112),1,6)='@time'";
		sql = sql.replace("@time", time);
		List<Map<String, Object>> list = jt.queryForList(sql);
		if (list.size() == 0) {
			return;
		}
		String table = "t_log_" + time;
		String create_table_sql = "create table @t_log ("
				+ "_id varchar(32) NOT NULL,company_id varchar(32) NULL,log varchar(200) NULL,"
				+ "level_msg varchar(40) NULL,clazz varchar(200) NULL,clazz_id varchar(32) NULL,"
				+ "user_id varchar(32) NULL,ip varchar(40) NULL,status int NULL,created datetime NULL,"
				+ "PRIMARY KEY (_id))";
		create_table_sql = create_table_sql.replace("@t_log", table);
		jt.execute(create_table_sql);
		selectAndCreate(table, time);
	}

	public static void main(String[] args) {
		LogService s = (LogService) SpringTools.getBean(LogService.class);
		// s.logLL("用户登录");
		s.createTable();
	}
}
